package com.li.net;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.io.OutputStreamWriter;
import java.io.PrintWriter;
import java.net.ServerSocket;
import java.net.Socket;
import java.util.concurrent.Callable;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

/**
 * 从技术上来讲，服务器Socket在服务器上运行，监听入站的TCP连接。数据总是通过常规的socket传输
 * 服务器程序的基本生命周期如下所示
 * 1、使用一个ServerSocket()构造函数在一个特定的端口创建一个新的ServerSocket()
 * 2、ServerSocket使用其accept方法监听这个端口的入站连接，accept会一直阻塞，直到一个客户端尝试建立连接
 * 此时accpect将返回一个连接客户端和服务器的socket对象
 * 3、根据服务器的类型，会调用Socket的getInputStream()方法和getOutputStream()方法，或者这两个方法同
 * 时调用，以获取和客户端通信的输入和输出流
 * 4、服务器和客户端根据已协商的协议交互，直到要关闭连接
 * 5、服务器和客户端关闭连接
 * 6、服务器返回到第二步，等待下一次连接
 * @author Administrator
 *
 */
public class ServerSocketDemo {
	
	private static ExecutorService pool;
	public static void main(String[] args) throws Exception {
		server();
	}

	public static void server() throws Exception{
		ServerSocket serverSocket=new ServerSocket(8088);
		Socket socket=serverSocket.accept();
		InputStream is = socket.getInputStream();
		BufferedReader br = new BufferedReader(new InputStreamReader(is));
		char[] data = new char[10];
		System.out.println("heheh");
		while(br.read(data) != -1){
			System.out.print(new String(data));
		}
		OutputStream os = socket.getOutputStream();
		BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(os));
		bw.write("欢迎你哦");
		bw.flush();
		bw.close();
		br.close();
		socket.close();
	}

	public static void testServerSocket() throws Exception{

	}

	public static void testServerSocketException(){
		try{
			ServerSocket serverSocket=new ServerSocket(8999);
			while(true){
				Socket socket=null;
				try {
					socket=serverSocket.accept();
					InputStream is=socket.getInputStream();
					BufferedReader br=new BufferedReader(new InputStreamReader(is));
					String data=null;
					StringBuilder result=new StringBuilder();
					while((data=br.readLine())!=null){
						result.append(data);
					}
					System.out.println(result.toString());
					OutputStream os=socket.getOutputStream();
					PrintWriter pw=new PrintWriter(os);
					pw.write("ok your message i am received!");
					pw.flush();
				} catch (IOException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}finally {
					socket.close();
				}

			}

		}catch(IOException e){
			e.printStackTrace();
		}
	}
	
	public static void muplictedThreadServerSocket() throws Exception{
		ServerSocket serverSocket=new ServerSocket(8888);
		while(true){
			Socket socket=null;
			socket=serverSocket.accept();
			Thread task=new Thread(new SocketThread(socket));
			task.start();
		}
	}
	
	public static void threadPoolServerSocket() throws Exception{
		ServerSocket serverSocket=new ServerSocket(8888);
		pool=Executors.newFixedThreadPool(20);
		Socket socket=serverSocket.accept();
		Callable<Void> task=new SocketCallable(socket);
		pool.submit(task);
	}
}
